﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Repository;
using RooHui.Infrastructure.Entities.Enum;

namespace RooHui.Infrastructure.Entities.Entity
{
    public class RepaymentPlan : IAccessible<P_RepaymentPlan, RepaymentPlan>, ILogicEntity<P_RepaymentPlan, RepaymentPlan>
    {
        public int ID { get; set; }
        public string UserName { get; set; }
        public int ProjectId { get; set; }
        public int PolicyId { get; set; }
        public decimal Rate { get; set; }
        public int CycleAmount { get; set; }
        public string StartDate { get; set; }
        public decimal RepaymentInterest { get; set; }
        public decimal RepaymentPrincipal { get; set; }
        public decimal RepaymentedInterest { get; set; }
        public decimal RepaymentedPrincipal { get; set; }
        public int SurplusDate { get; set; }
        public string EndDate { get; set; }
        public DateTime CreateTime { get; set; }
        public RepaymentStatus Status { get; set; }
        public Policy Policy { get; set; }

        public List<RepaymentPlanItem> RepaymentPlanItemList { get; set; }

        //与表无关
        public decimal BorrowTotalInterest { get; set; }
        public decimal InvestTotalInterest { get; set; }
        public decimal GuaranteeTotalInterest { get; set; }

        public RepaymentPlan LoadPolicy(P_Policy p_policy)
        {
            this.Policy = new Policy().Set(p_policy);
            return this;
        }

        protected override void Covariant(P_RepaymentPlan model)
        {
            if (model != null)
            {
                this.CreateTime = model.CreateTime;
                this.CycleAmount = model.CycleAmount;
                this.EndDate = model.EndDate;
                this.ID = model.ID;
                this.PolicyId = model.PolicyId;
                this.ProjectId = model.ProjectId; 
                this.Rate = model.Rate;
                this.RepaymentedInterest = model.RepaymentedInterest;
                this.RepaymentedPrincipal = model.RepaymentedPrincipal;
                this.RepaymentInterest = model.RepaymentInterest;
                this.RepaymentPrincipal = model.RepaymentPrincipal;
                this.StartDate = model.StartDate;
                this.Status = (RepaymentStatus)model.Status;
                this.SurplusDate = model.SurplusDate;
                this.UserName = model.UserName;
            }
        }

        protected override P_RepaymentPlan Inverter(RepaymentPlan model)
        {
            if (model != null)
            {
                return new P_RepaymentPlan()
                {
                    CreateTime = model.CreateTime,
                    CycleAmount = model.CycleAmount,
                    EndDate = model.EndDate,
                    ID = model.ID,
                    PolicyId = model.PolicyId,
                    ProjectId = model.ProjectId, 
                    Rate = model.Rate,
                    RepaymentedInterest = model.RepaymentedInterest,
                    RepaymentedPrincipal = model.RepaymentedPrincipal,
                    RepaymentInterest = model.RepaymentInterest,
                    RepaymentPrincipal = model.RepaymentPrincipal,
                    StartDate = model.StartDate,
                    Status = (int)model.Status,
                    SurplusDate = model.SurplusDate,
                    UserName = model.UserName
                };
            }
            return null;
        }

        public RepaymentPlan Set(P_RepaymentPlan dbModel)
        {
            Covariant(dbModel);
            return this;
        }
    }
}
